package com.holly.common_view.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.GridView;

import com.holly.common_view.R;

/**
 * 2016/11/8 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class DividerGridView extends GridView {
    public DividerGridView(Context context) {
        super(context);
    }

    public DividerGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DividerGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        View localView1 = getChildAt(0);
        if(null == localView1){
            return;
        }
        int column = getWidth() / localView1.getWidth();//计算出一共有多少列，
        int childCount = getChildCount();//子view的总数
        System.out.println("子view的总数childCount==" + childCount);
        Paint localPaint;//画笔
        localPaint = new Paint();
        int paintSize = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
        localPaint.setTextSize(paintSize);
//        localPaint.setStyle(Paint.Style.STROKE);
        localPaint.setColor(getContext().getResources().getColor(R.color.divider));//设置画笔的颜色
        for (int i = 0; i < childCount; i++) {//遍历子view
            View cellView = getChildAt(i);//获取子view
            if(i%2 == 0){
                canvas.drawLine(cellView.getLeft(), cellView.getBottom()-(float) Math.floor(i/2)-paintSize, cellView.getRight(), cellView.getBottom()-(float) Math.floor(i/2)-paintSize, localPaint);
                canvas.drawLine(cellView.getRight(), cellView.getBottom() - (float) Math.floor(i/2)-paintSize, cellView.getRight(), cellView.getTop(), localPaint);
            }else{
                canvas.drawLine(cellView.getLeft(), cellView.getBottom()-(float) Math.floor(i/2)-paintSize, cellView.getRight(), cellView.getBottom()-(float) Math.floor(i/2)-paintSize, localPaint);
            }
        }
    }
}
